package com.mq.task;

import com.alibaba.fastjson.JSON;
import com.common.entity.Kill;
import com.common.entity.Programme;
import com.common.entity.Rsp;
import com.mq.feign.KillFeign;
import com.mq.feign.ProgrammeFeign;
import com.mq.mqtt.MqttService;
import com.mq.service.RedisServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 路径：com.example.demo.service
 * 类名：
 * 功能：任务类
 * 备注：
 * 创建人：hcq
 * 创建时间：2018/10/10 12:03
 * 修改人：
 * 修改备注：
 * 修改时间：
 */
@Slf4j
@Configuration
@Component
@EnableScheduling
public class ScheduledTask {

    @Autowired
    ProgrammeFeign programmeFeign;

    @Autowired
    KillFeign killFeign;

    @Autowired
    RedisServiceImpl redisService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    public void onAccess(){
        log.info("任务：实时监控");

        List<Kill> kills = new ArrayList<>(3);
        boolean exists = redisService.exists("plan");
        if(!exists){
            //读取所有的方案
            List<Programme> programmes = programmeFeign.searchAllProgramme();
            System.out.println(programmes);

            for(Programme programme : programmes){
                redisService.lPush("plan",programme);
            }
        }

        //对比redis中的库存
        log.info("对比redis中的库存");
        List<Object> redis =  redisService.lRange("plan",0L,-1L);
        System.out.println(redis);


        for (int i = 0; i <redis.size() ; i++) {
            Programme programme = (Programme) redis.get(i);
            Integer stock = programme.getStock();

            Rsp rsp = killFeign.findByGoodsId(programme.getId());
            System.out.println(rsp);
            if(rsp.getCode() == 20000){
                continue;
            }
            //如果库存不足20件 开始秒杀
            if (stock <= 20){
                Kill kill = new Kill();
                kill.setId(UUID.randomUUID().toString().replaceAll("-",""));
                kill.setGoodsId(programme.getId());
                kill.setName(programme.getName());
                kill.setSellPrice(programme.getPrice());
                kill.setKillPrice(programme.getPrice().multiply(new BigDecimal("0.8")));
                kill.setSellStock(programme.getStock());
                kill.setCreateTime(new Date());
                kill.setStartTime(new Date());
                kill.setEndTime(new Date());
                kills.add(kill);
            }
        }
        if (kills.size() == 0){
            System.out.println("没有秒杀商品");
            return;
        }
        System.out.println(kills);
        Rsp rsp = killFeign.addMany(kills);

        //构建请求
//        MqttService service = MqttService.builder()
//                .host("tcp://106.15.35.62:1883")
//                .userName("rabbitmq")
//                .passWord("123456")
//                .clientId("myclientid_10001")
//                .defaultTopic("MIDDOL-TEST")
//                .cleanSession(true).build();

        if (rsp.getCode() == 20000){
            boolean flag = redisService.exists("kill");
            if(flag){
                redisService.remove("kill");
            }
            for(Kill kill : kills){
                redisService.lPush("kill",kill);
            }
            String s = JSON.toJSONString(kills);
            rabbitTemplate.convertAndSend("exchange_pushmsg","rk_pushmsg",s);
        }else {
            rabbitTemplate.convertAndSend("exchange_pushmsg","rk_pushmsg","50000");
        }

    }
}
